package jTorrent;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import org.apache.xmlrpc.XmlRpcException;

public class ConnectPanel extends JPanel implements ActionListener
{
	GUI gui;
    /**
	 * 
	 */
	private static final long serialVersionUID = -2545641049909529443L;
	JLabel host;
    JLabel user;
    JLabel pass;
    JTextField hostTxt;
    JTextField userTxt;
    JPasswordField passTxt;
    JButton button;
    
	public ConnectPanel(GUI gui)
	{
		this.gui = gui;
        host = new JLabel("URL :", JLabel.TRAILING);
        user = new JLabel("User :", JLabel.TRAILING);
        pass = new JLabel("Pass :", JLabel.TRAILING);
        hostTxt = new JTextField(20);
        hostTxt.setSize(100, 0);
        userTxt = new JTextField(10);
        passTxt = new JPasswordField(10);
        button = new JButton("Connect");
        
        button.addActionListener(this);
        
        setLayout(new SpringLayout());
        
        add(host);
        host.setLabelFor(hostTxt);
        add(hostTxt);
        add(user);
        user.setLabelFor(userTxt);
        add(userTxt);
        add(pass);
        pass.setLabelFor(passTxt);
        add(passTxt);
        JLabel blub = new JLabel();
        add(blub);
        blub.setLabelFor(button);
        add(button);
        setVisible(true);
        layout.SpringUtilities.makeCompactGrid(this,
                4, 2, //rows, cols
                6, 6,        //initX, initY
                6, 6);       //xPad, yPad
	}
    

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
		if(arg0.getSource() == button)
		{
			XMLRPCFunctions xmlRPCFunctions = new XMLRPCFunctions();
			try
			{
				xmlRPCFunctions.estConnection(new URL(hostTxt.getText()), userTxt.getText(), String.valueOf(passTxt.getPassword()));
			}
			catch (MalformedURLException e)
			{
				System.out.println("MalFormedURL, aborting creation of Torrentsession ["+ hostTxt.getText() +"]");
				JOptionPane.showMessageDialog(this, "No correct URL-Format, aborting.", "Error", JOptionPane.ERROR_MESSAGE);
				xmlRPCFunctions = null;
				
				return;
			}
			
			if(xmlRPCFunctions.getTorrentList() == XmlRpcException.class)
			{
				System.out.println("Connection refused, aborting creation of Torrentsession ["+ hostTxt.getText() +"]");
				JOptionPane.showMessageDialog(this, "Connection refused, aborting.", "Error", JOptionPane.ERROR_MESSAGE);
				xmlRPCFunctions = null;
			}
			else
			{
				int index = gui.tabbedPane.getSelectedIndex();
				gui.tabbedPane.setComponentAt(index, (JComponent) new TablePanel(xmlRPCFunctions));
			}
			
		}
	}
}

